


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > JPacmanFuzzer</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.fuzzer</a>
</div>

<h1>Coverage Summary for Class: JPacmanFuzzer (nl.tudelft.jpacman.fuzzer)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">JPacmanFuzzer</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/7)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/33)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.fuzzer;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import nl.tudelft.jpacman.Launcher;
<i class="no-highlight">4</i>&nbsp;import nl.tudelft.jpacman.board.Direction;
<i class="no-highlight">5</i>&nbsp;import nl.tudelft.jpacman.game.Game;
<i class="no-highlight">6</i>&nbsp;import nl.tudelft.jpacman.level.Player;
<i class="no-highlight">7</i>&nbsp;import org.junit.jupiter.api.AfterEach;
<i class="no-highlight">8</i>&nbsp;import org.junit.jupiter.api.BeforeAll;
<i class="no-highlight">9</i>&nbsp;import org.junit.jupiter.api.BeforeEach;
<i class="no-highlight">10</i>&nbsp;import org.junit.jupiter.api.Disabled;
<i class="no-highlight">11</i>&nbsp;import org.junit.jupiter.api.RepeatedTest;
<i class="no-highlight">12</i>&nbsp;import org.junit.jupiter.api.RepetitionInfo;
<i class="no-highlight">13</i>&nbsp;
<i class="no-highlight">14</i>&nbsp;import java.io.BufferedWriter;
<i class="no-highlight">15</i>&nbsp;import java.io.File;
<i class="no-highlight">16</i>&nbsp;import java.io.FileOutputStream;
<i class="no-highlight">17</i>&nbsp;import java.io.IOException;
<i class="no-highlight">18</i>&nbsp;import java.io.OutputStreamWriter;
<i class="no-highlight">19</i>&nbsp;import java.nio.charset.StandardCharsets;
<i class="no-highlight">20</i>&nbsp;import java.util.Random;
<i class="no-highlight">21</i>&nbsp;
<i class="no-highlight">22</i>&nbsp;/**
<i class="no-highlight">23</i>&nbsp; * A basic fuzzer trying out random moves and writing logs in output files. Number of runs is a
<i class="no-highlight">24</i>&nbsp; * tunable parameter. Each run initiates the game and is played until PacMan dies.
<i class="no-highlight">25</i>&nbsp; *
<i class="no-highlight">26</i>&nbsp; * @author Azqa Nadeem
<i class="no-highlight">27</i>&nbsp; */
<i class="no-highlight">28</i>&nbsp;@Disabled
<b class="nc"><i class="no-highlight">29</i>&nbsp;class JPacmanFuzzer {</b>
<i class="no-highlight">30</i>&nbsp;    private static final int RUNS = 5;
<i class="no-highlight">31</i>&nbsp;    private static final String LOG_HEADER =
<i class="no-highlight">32</i>&nbsp;        &quot;isAlive, hasCollided, currentDirection, nextDirection, remainingPellets, score&quot;;
<i class="no-highlight">33</i>&nbsp;    private static final String BASE_LOG_DIRECTORY = &quot;behavioral-analysis/logs&quot;;
<i class="no-highlight">34</i>&nbsp;
<i class="no-highlight">35</i>&nbsp;    private static File logDirectory;
<i class="no-highlight">36</i>&nbsp;
<i class="no-highlight">37</i>&nbsp;    private Launcher launcher;
<i class="no-highlight">38</i>&nbsp;
<i class="no-highlight">39</i>&nbsp;    /**
<i class="no-highlight">40</i>&nbsp;     * Sets and cleans up directory for storing logs.
<i class="no-highlight">41</i>&nbsp;     *
<i class="no-highlight">42</i>&nbsp;     * @throws IOException when file/folder can&#39;t be created.
<i class="no-highlight">43</i>&nbsp;     */
<i class="no-highlight">44</i>&nbsp;    @BeforeAll
<i class="no-highlight">45</i>&nbsp;    static void setUpLogging() throws IOException {
<b class="nc"><i class="no-highlight">46</i>&nbsp;        logDirectory = new File(BASE_LOG_DIRECTORY, Long.toString(System.currentTimeMillis()));</b>
<b class="nc"><i class="no-highlight">47</i>&nbsp;        if (!logDirectory.exists() &amp;&amp; !logDirectory.mkdirs()) {</b>
<b class="nc"><i class="no-highlight">48</i>&nbsp;            throw new IOException(&quot;Directory `behavioral-analysis/` could not be created.&quot;);</b>
<i class="no-highlight">49</i>&nbsp;        }
<i class="no-highlight">50</i>&nbsp;    }
<i class="no-highlight">51</i>&nbsp;
<i class="no-highlight">52</i>&nbsp;    /**
<i class="no-highlight">53</i>&nbsp;     * Launch the user interface.
<i class="no-highlight">54</i>&nbsp;     */
<i class="no-highlight">55</i>&nbsp;    @BeforeEach
<i class="no-highlight">56</i>&nbsp;    void setUpGame() {
<b class="nc"><i class="no-highlight">57</i>&nbsp;        launcher = new Launcher();</b>
<b class="nc"><i class="no-highlight">58</i>&nbsp;        launcher.launch();</b>
<i class="no-highlight">59</i>&nbsp;    }
<i class="no-highlight">60</i>&nbsp;
<i class="no-highlight">61</i>&nbsp;    /**
<i class="no-highlight">62</i>&nbsp;     * Quit the user interface when we&#39;re done.
<i class="no-highlight">63</i>&nbsp;     *
<i class="no-highlight">64</i>&nbsp;     * @throws IOException when log writer cannot be closed.
<i class="no-highlight">65</i>&nbsp;     */
<i class="no-highlight">66</i>&nbsp;    @AfterEach
<i class="no-highlight">67</i>&nbsp;    void tearDown() throws IOException {
<b class="nc"><i class="no-highlight">68</i>&nbsp;        launcher.dispose();</b>
<i class="no-highlight">69</i>&nbsp;    }
<i class="no-highlight">70</i>&nbsp;
<i class="no-highlight">71</i>&nbsp;    /**
<i class="no-highlight">72</i>&nbsp;     * Basic fuzzer implementation.
<i class="no-highlight">73</i>&nbsp;     *
<i class="no-highlight">74</i>&nbsp;     * @param repetitionInfo repeated test information
<i class="no-highlight">75</i>&nbsp;     * @throws IOException when the log write created.
<i class="no-highlight">76</i>&nbsp;     */
<i class="no-highlight">77</i>&nbsp;    @RepeatedTest(RUNS)
<i class="no-highlight">78</i>&nbsp;    void fuzzerTest(RepetitionInfo repetitionInfo) throws IOException {
<b class="nc"><i class="no-highlight">79</i>&nbsp;        Game game = launcher.getGame();</b>
<b class="nc"><i class="no-highlight">80</i>&nbsp;        Direction chosen = Direction.EAST;</b>
<i class="no-highlight">81</i>&nbsp;
<b class="nc"><i class="no-highlight">82</i>&nbsp;        String logFileName = &quot;log_&quot; + repetitionInfo.getCurrentRepetition() + &quot;.txt&quot;;</b>
<b class="nc"><i class="no-highlight">83</i>&nbsp;        File logFile = new File(logDirectory, logFileName);</b>
<i class="no-highlight">84</i>&nbsp;
<b class="nc"><i class="no-highlight">85</i>&nbsp;        try (BufferedWriter logWriter = new BufferedWriter(new OutputStreamWriter(</b>
<i class="no-highlight">86</i>&nbsp;            new FileOutputStream(logFile, true), StandardCharsets.UTF_8))) {
<i class="no-highlight">87</i>&nbsp;
<b class="nc"><i class="no-highlight">88</i>&nbsp;            logWriter.write(LOG_HEADER);</b>
<i class="no-highlight">89</i>&nbsp;
<i class="no-highlight">90</i>&nbsp;            try {
<b class="nc"><i class="no-highlight">91</i>&nbsp;                game.start();</b>
<i class="no-highlight">92</i>&nbsp;
<b class="nc"><i class="no-highlight">93</i>&nbsp;                while (game.isInProgress()) {</b>
<b class="nc"><i class="no-highlight">94</i>&nbsp;                    chosen = getRandomDirection();</b>
<i class="no-highlight">95</i>&nbsp;
<b class="nc"><i class="no-highlight">96</i>&nbsp;                    log(logWriter, chosen);</b>
<b class="nc"><i class="no-highlight">97</i>&nbsp;                    game.getLevel().move(game.getPlayers().get(0), chosen);</b>
<i class="no-highlight">98</i>&nbsp;                }
<b class="nc"><i class="no-highlight">99</i>&nbsp;            } catch (RuntimeException e) {</b>
<i class="no-highlight">100</i>&nbsp;                // Runtime exceptions should not stop the execution of the fuzzer
<i class="no-highlight">101</i>&nbsp;            } finally {
<b class="nc"><i class="no-highlight">102</i>&nbsp;                log(logWriter, chosen);</b>
<b class="nc"><i class="no-highlight">103</i>&nbsp;                game.stop();</b>
<b class="nc"><i class="no-highlight">104</i>&nbsp;            }</b>
<b class="nc"><i class="no-highlight">105</i>&nbsp;        }</b>
<i class="no-highlight">106</i>&nbsp;    }
<i class="no-highlight">107</i>&nbsp;
<i class="no-highlight">108</i>&nbsp;    private Direction getRandomDirection() {
<b class="nc"><i class="no-highlight">109</i>&nbsp;        return Direction.values()[new Random().nextInt(Direction.values().length)];</b>
<i class="no-highlight">110</i>&nbsp;    }
<i class="no-highlight">111</i>&nbsp;
<i class="no-highlight">112</i>&nbsp;    private void log(BufferedWriter logWriter, Direction chosen) throws IOException {
<b class="nc"><i class="no-highlight">113</i>&nbsp;        Game game = launcher.getGame();</b>
<b class="nc"><i class="no-highlight">114</i>&nbsp;        Player player = game.getPlayers().get(0);</b>
<i class="no-highlight">115</i>&nbsp;
<b class="nc"><i class="no-highlight">116</i>&nbsp;        logWriter.write(</b>
<b class="nc"><i class="no-highlight">117</i>&nbsp;            String.format(</b>
<i class="no-highlight">118</i>&nbsp;                &quot;%n%b %b %s %s %d %d&quot;,
<b class="nc"><i class="no-highlight">119</i>&nbsp;                player.isAlive(),</b>
<b class="nc"><i class="no-highlight">120</i>&nbsp;                (player.getKiller() != null),</b>
<b class="nc"><i class="no-highlight">121</i>&nbsp;                player.getDirection(),</b>
<i class="no-highlight">122</i>&nbsp;                chosen,
<b class="nc"><i class="no-highlight">123</i>&nbsp;                game.getLevel().remainingPellets(),</b>
<b class="nc"><i class="no-highlight">124</i>&nbsp;                player.getScore()</b>
<i class="no-highlight">125</i>&nbsp;            )
<i class="no-highlight">126</i>&nbsp;        );
<i class="no-highlight">127</i>&nbsp;    }
<i class="no-highlight">128</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
